package com.chakid.dbf;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class DBConnect {
	/**
	 * 驱动类
	 */
	public static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	
//	public static String  JDBC_DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	/**
	 * 连接驱动符
	 */
//	public static String DB_URL = "jdbc:mysql://localhost:3306/test";
	public static String DB_URL = "jdbc:mysql://119.29.85.115:3306/hbwl?characterEncoding=UTF-8";
	
//	public static String DB_URL="jdbc:sqlserver://localhost:1433;DatabaseName=db_tdd";
	/**
	 * 用户名
	 */
	public static String USER = "hbwl";
//	public static String USER = "root";
	/**
	 * 密码
	 */
	public static String PASS = "123456";
//	public static String PASS = "localhost123";

	public static Connection getConnection() {

		Connection conn = null;

		try {
			Class.forName(JDBC_DRIVER);
			// 打开链接
			System.out.println("连接数据库...");
			return conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 关闭数据库
	 * 
	 * @param rs
	 *            返回结果记录集
	 * @param stm
	 *            执行sql操作
	 * @param con
	 *            建立连接
	 */
	public static void close(ResultSet rs, java.sql.Statement stm, Connection con) {

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (stm != null) {
					try {
						stm.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} finally {
						if (con != null) {
							try {
								con.close();
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		}

	}
}
